#include <Arduino.h>
#define BUZZER_PIN 23
#define MAX_TIME 5000

int last_time = 0;

const char *receiveCode = "LC:66220991";

void alarm(int pin)
{
  digitalWrite(pin, 1);
  delay(200);
  digitalWrite(pin, 0);
  delay(500);
  digitalWrite(pin, 1);
  delay(200);
  digitalWrite(pin, 0);
}

void setup()
{
  Serial.begin(115200);
  Serial2.begin(9600);
  pinMode(BUZZER_PIN, OUTPUT);
}

void loop()
{
  // 如果从无线模块的串口接收到消息
  if (Serial2.available() > 0)
  {
    String signal = Serial2.readStringUntil('\n');
    signal.replace("\r", ""); // 移除回车符
    if (signal == receiveCode)
    {
      int current_time = millis();
      if ((current_time - last_time) > MAX_TIME)
      {
        last_time = current_time;
        alarm(BUZZER_PIN);
      }
    }
  }
}